package com.neusoft.hifly.push.sms.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.push.sms.aliyun.pojo.SendDetailVO;
import com.neusoft.hifly.push.sms.aliyun.service.IAliSmsSendService;
import com.neusoft.hifly.push.sms.gateway.pojo.SMSToDoVO;
import com.neusoft.hifly.push.sms.gateway.service.ISMSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Title: 短信服务
 * <p>
 * Description: 发送短息接口
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1/sms")
@Api(value = "sms-api", description = "短信发送接口")
public class SmsApi {

	@Autowired(required = false)
	private IAliSmsSendService aliSmsSendService;

	@Autowired(required = false)
	private ISMSService smsService;

	/**
	 * 阿里大于发送短信
	 *
	 * @param smsVO
	 *            短信实体对象JSON串
	 * @return 是否成功
	 */
	@ApiOperation("发送短信")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "smsVO", value = "短信实体对象JSON串，例如：{\"accessKeyId\": \"LTAIYqjQ10i0dSGl\",\"accessKeySecret\": \"PuRtUKM6KcA6aAu9r8dBeshDwD6TWd\",\"phoneNumber\": \"18612345678\",\"signName\": \"畅通数据\",\"templateCode\": \"SMS_126866102\",\"templateParam\": {\\\"code\\\":\\\"1234\\\"}}", dataType = "SendDetailVO", required = true) })
	@PostMapping(value = "/send/aliyun")
	public ApiResponse<?> sendByAliyun(@RequestBody final SendDetailVO smsVO) {
		if (smsVO == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误！");
		}
		Log.info("阿里大于短信发送：" + JSON.toJSONString(smsVO));
		try {
			final SendSmsResponse sendSmsResponse = aliSmsSendService.sendSms(smsVO);
			return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "发送成功！", sendSmsResponse);
		} catch (final ClientException e) {
			e.printStackTrace();
			return new ApiResponse<>(ApiCode.ERROR.getCode(), e.getMessage());
		}

	}

	/**
	 * 短信网管发送短信
	 *
	 * @param smsVO
	 *            短信实体对象JSON串
	 * @return 是否成功
	 */
	@ApiOperation("发送短信")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "smsVO", value = "短信实体对象JSON串，例如：{\"organLogicId\": \"1234567890\",\"sender\": \"畅通数据\",\"receiver\": \"18624001180\",\"message\": \"测试短信\"}", dataType = "SendDetailVO", required = true) })
	@PostMapping(value = "/send/gateway")
	public ApiResponse<?> sendByGateway(@RequestBody final SMSToDoVO smsVO) {
		if (smsVO == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误！");
		}
		Log.info("短信网管短信发送：" + JSON.toJSONString(smsVO));
		final List<String> receivers = new ArrayList<String>(1);
		receivers.add(smsVO.getReceiver());

		smsService.sendMessage(smsVO.getSender(), receivers, smsVO.getMessage(), smsVO.getOrganLogicId());

		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "发送成功！");

	}
}
